package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.Customer;
import com.lutzyt.delivery.bean.Horseman;
import com.lutzyt.delivery.bean.HorsemanComment;
import com.lutzyt.delivery.bean.Order;
import com.lutzyt.delivery.mapper.HorsemanCommentDAO;
import com.lutzyt.delivery.service.HorsemanCommentService;
import com.lutzyt.delivery.service.HorsemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HorsemanCommentServiceImpl extends ServiceImpl<HorsemanCommentDAO, HorsemanComment> implements HorsemanCommentService {

  private HorsemanCommentDAO horsemanCommentDAO;
  private HorsemanService horsemanService;

  @Transactional(readOnly = true)
  @Override
  public HorsemanComment findById(Integer id) {
    return horsemanCommentDAO.selectById(id);
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<HorsemanComment> findByPage(int pageNum, int pageSize) {
    return horsemanCommentDAO.selectPage(new Page<>(pageNum, pageSize), null);
  }

  @Override
  public void insert(HorsemanComment horsemanComment) {
    horsemanCommentDAO.insert(horsemanComment);
  }

  @Override
  public void update(HorsemanComment horsemanComment) {
    horsemanCommentDAO.updateById(horsemanComment);
  }

  @Override
  public void deleteById(Integer id) {
    horsemanCommentDAO.deleteById(id);
  }

  @Override
  public List<HorsemanComment> findByCustomerId(Integer id) {
    final List<HorsemanComment> list = this.lambdaQuery().eq(HorsemanComment::getCustomerId, id)
        .eq(HorsemanComment::getDeleted, 0)
        .list();
    list.forEach(this::setHorseman);
    return list;
  }

  @Override
  public List<HorsemanComment> findByHorsemanId(Integer id) {
    final List<HorsemanComment> list = this.lambdaQuery()
        .eq(HorsemanComment::getHorsemanId, id)
        .eq(HorsemanComment::getDeleted, 0)
        .eq(HorsemanComment::getState, "PASS")
        .list();
    return list;
  }

  private void setHorseman(HorsemanComment horsemanComment){
    final Horseman horseman = horsemanService.findById(horsemanComment.getHorsemanId());
    horsemanComment.setHorseman(horseman);
  }

  @Override
  public String getAuditField() {
    return "state";
  }

  @Autowired
  public void setHorsemanService(HorsemanService horsemanService) {
    this.horsemanService = horsemanService;
  }

  @Autowired
  public void setHorsemanCommentDAO(HorsemanCommentDAO horsemanCommentDAO) {
    this.horsemanCommentDAO = horsemanCommentDAO;
  }
}